package org.jasig.services.persondir.support.jdbc;

import org.jasig.services.persondir.IPersonAttributes;
import org.jasig.services.persondir.support.AttributeNamedPersonImpl;
import org.jasig.services.persondir.support.StubPersonAttributeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class CustomerRowJdbcPersonAttributeDao extends StubPersonAttributeDao {
	
	private static JdbcTemplate simpleJdbcTemplate;

	private static String sql;
	public CustomerRowJdbcPersonAttributeDao(DataSource ds)
    {
        simpleJdbcTemplate = new JdbcTemplate(ds);
    }

	public IPersonAttributes getPerson(String uid)
    {
    	Map<String, List<Object>> attributes = new HashMap<String, List<Object>>();
		List list = simpleJdbcTemplate.query(this.sql, new Object[]{uid}, new BeanPropertyRowMapper<UserInfo>(UserInfo.class));
    	attributes.put("user_info", list);
        return new AttributeNamedPersonImpl(attributes);
    }

	@Autowired
	public void setSql(@Value("${cas.jdbc.userinfo.query.sql:}") final String sql) {
		this.sql = sql;
	}


}

